<template>
  <view class="container">
    <!-- 商品信息 -->
    <view class="product-section">
      <view class="product-item" v-for="(item, index) in orderInfo.products" :key="index">
        <image :src="item.image" mode="aspectFill"></image>
        <view class="product-info">
          <text class="title">{{item.title}}</text>
          <text class="specs">{{item.specs}}</text>
        </view>
      </view>
    </view>
    
    <!-- 评分 -->
    <view class="rating-section">
      <text class="label">商品评分</text>
      <view class="rating">
        <text 
          class="iconfont" 
          :class="rating >= index + 1 ? 'icon-star-fill' : 'icon-star'" 
          v-for="(item, index) in 5" 
          :key="index"
          @tap="setRating(index + 1)"
        ></text>
      </view>
    </view>
    
    <!-- 评价内容 -->
    <view class="content-section">
      <textarea 
        v-model="content" 
        placeholder="请输入您的评价内容" 
        :maxlength="500"
        show-confirm-bar
      ></textarea>
      <text class="count">{{content.length}}/500</text>
    </view>
    
    <!-- 图片上传 -->
    <view class="upload-section">
      <text class="label">上传图片</text>
      <view class="upload-list">
        <view 
          class="upload-item" 
          v-for="(item, index) in images" 
          :key="index"
        >
          <image :src="item" mode="aspectFill"></image>
          <text class="iconfont icon-close" @tap="deleteImage(index)"></text>
        </view>
        <view class="upload-btn" @tap="chooseImage" v-if="images.length < 9">
          <text class="iconfont icon-camera"></text>
          <text>上传图片</text>
        </view>
      </view>
      <text class="tip">最多上传9张图片</text>
    </view>
    
    <!-- 匿名评价 -->
    <view class="anonymous-section">
      <text>匿名评价</text>
      <switch :checked="isAnonymous" @change="toggleAnonymous" color="#ff0000" />
    </view>
    
    <!-- 提交按钮 -->
    <view class="submit-section">
      <button class="submit-btn" @tap="submitEvaluate">提交评价</button>
    </view>
  </view>
</template>

<script>
import { orderApi } from '@/api/index'
import { showToast } from '@/utils/common'

export default {
  data() {
    return {
      orderId: '',
      orderInfo: {
        products: []
      },
      rating: 5,
      content: '',
      images: [],
      isAnonymous: false
    }
  },
  onLoad(options) {
    this.orderId = options.orderId
    this.getOrderDetail()
  },
  methods: {
    // 获取订单详情
    async getOrderDetail() {
      try {
        const res = await orderApi.getOrderDetail(this.orderId)
        this.orderInfo = res
      } catch (error) {
        showToast('订单信息加载失败')
      }
    },
    
    // 设置评分
    setRating(value) {
      this.rating = value
    },
    
    // 选择图片
    chooseImage() {
      uni.chooseImage({
        count: 9 - this.images.length,
        success: (res) => {
          this.images = [...this.images, ...res.tempFilePaths]
        }
      })
    },
    
    // 删除图片
    deleteImage(index) {
      this.images.splice(index, 1)
    },
    
    // 切换匿名评价
    toggleAnonymous(e) {
      this.isAnonymous = e.detail.value
    },
    
    // 提交评价
    async submitEvaluate() {
      if (this.rating === 0) {
        showToast('请选择评分')
        return
      }
      
      if (!this.content.trim()) {
        showToast('请输入评价内容')
        return
      }
      
      try {
        // 上传图片
        const uploadedImages = []
        for (const image of this.images) {
          const res = await orderApi.uploadImage(image)
          uploadedImages.push(res.url)
        }
        
        // 提交评价
        await orderApi.submitEvaluate({
          orderId: this.orderId,
          rating: this.rating,
          content: this.content,
          images: uploadedImages,
          isAnonymous: this.isAnonymous
        })
        
        showToast('评价提交成功')
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      } catch (error) {
        showToast('评价提交失败')
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 120rpx;
}

.product-section {
  background: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .product-item {
    display: flex;
    margin-bottom: 20rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    image {
      width: 160rpx;
      height: 160rpx;
      border-radius: 8rpx;
      margin-right: 20rpx;
    }
    
    .product-info {
      flex: 1;
      
      .title {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
      }
      
      .specs {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
}

.rating-section {
  background: #fff;
  padding: 30rpx;
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
  
  .label {
    font-size: 28rpx;
    color: #333;
    margin-right: 30rpx;
  }
  
  .rating {
    .iconfont {
      font-size: 40rpx;
      color: #ddd;
      margin-right: 10rpx;
      
      &.icon-star-fill {
        color: #ff0000;
      }
    }
  }
}

.content-section {
  background: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  position: relative;
  
  textarea {
    width: 100%;
    height: 200rpx;
    font-size: 28rpx;
    color: #333;
  }
  
  .count {
    position: absolute;
    right: 30rpx;
    bottom: 30rpx;
    font-size: 24rpx;
    color: #999;
  }
}

.upload-section {
  background: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .label {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .upload-list {
    display: flex;
    flex-wrap: wrap;
    margin: 0 -10rpx;
    
    .upload-item {
      position: relative;
      width: 200rpx;
      height: 200rpx;
      margin: 10rpx;
      
      image {
        width: 100%;
        height: 100%;
        border-radius: 8rpx;
      }
      
      .icon-close {
        position: absolute;
        right: -10rpx;
        top: -10rpx;
        width: 40rpx;
        height: 40rpx;
        line-height: 40rpx;
        text-align: center;
        background: rgba(0, 0, 0, 0.5);
        color: #fff;
        border-radius: 50%;
        font-size: 24rpx;
      }
    }
    
    .upload-btn {
      width: 200rpx;
      height: 200rpx;
      margin: 10rpx;
      background: #f5f5f5;
      border-radius: 8rpx;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      
      .icon-camera {
        font-size: 48rpx;
        color: #999;
        margin-bottom: 10rpx;
      }
      
      text {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
  
  .tip {
    font-size: 24rpx;
    color: #999;
    margin-top: 20rpx;
  }
}

.anonymous-section {
  background: #fff;
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  text {
    font-size: 28rpx;
    color: #333;
  }
}

.submit-section {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 30rpx;
  background: #fff;
  
  .submit-btn {
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #ff0000;
    color: #fff;
    border-radius: 40rpx;
    font-size: 28rpx;
  }
}
</style> 